package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import core.corewa.Event;
import db.dao.DictionariesDAO;
import db.dao.LanguagesDAO;
import db.dao.WordsDAO;
import db.dbo.ListDictionariesDBO;
import db.dto.DictionariesDTO;
import db.dto.LanguagesDTO;
import db.dto.WordsDTO;
import form.ApproverHomeForm;
import form.BaseForm;
import form.ReviewerCheckingForm;
import form.ReviewerHomeForm;

public class ReviewerCheckingController extends BaseController {
	/**
	 * logout function.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		request.getPortletSession().removeAttribute("username", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().removeAttribute("role", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("username", "Guest",PortletSession.APPLICATION_SCOPE);
    	request.getPortletSession().setAttribute("role", -1, PortletSession.APPLICATION_SCOPE);
		return null;
	}
	/**
	 * do nothing in here.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procHome(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		return null;
	}
	/**
	 * function for action that reviewer submit Back button.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procBack(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String username = (String) request.getPortletSession().getAttribute("username", PortletSession.APPLICATION_SCOPE);
		int role = (Integer) request.getPortletSession().getAttribute("role", PortletSession.APPLICATION_SCOPE);
		ListDictionariesDBO listDict = new ListDictionariesDBO();
		if(role == 1) {
			event.setNextScrId("/WEB-INF/jsp/ReviewerHome.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
	    	ReviewerHomeForm reviewerForm = new ReviewerHomeForm();
	    	
	    	reviewerForm.setListDictionaries(listDict.getListDictionariesReviewer(username));
	    	return reviewerForm;
		}
		if(role == 2) {
			event.setNextScrId("/WEB-INF/jsp/ApproverHome.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
	    	ApproverHomeForm approverForm = new ApproverHomeForm();
	    	approverForm.setListDictionaries(listDict.getListDictionariesApprover(username));
	    	return approverForm;
		}
		return null;
	}
	/**
	 * fucntion perform when reviewer submit approve button.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procApprove(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String[] ids = request.getParameterValues("wordID");
		WordsDAO wordDAO = new WordsDAO();
		for(int i=0; i < ids.length; i++) {
			wordDAO.setStatusWord(Integer.parseInt(ids[i]));
		}
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		List<WordsDTO> listWord = wordDAO.getWord(dictId);
		ReviewerCheckingForm reviewerCheckingForm = new ReviewerCheckingForm();
		List<WordsDTO> listNewWords = new ArrayList<WordsDTO>();
		for (WordsDTO wordsDTO : listWord) {
			if(wordsDTO.getStatus() == 0) {
				listNewWords.add(wordsDTO);
			}
		}
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from;
		LanguagesDTO to;
		int fromId = dictDTO.getFrom();
		int toId = dictDTO.getTo();
		from = lanDAO.getLanguage(fromId);
		to = lanDAO.getLanguage(toId);
		reviewerCheckingForm.setDictName(from.getName()+" - "+to.getName());
		reviewerCheckingForm.setListNewWords(listNewWords);
		reviewerCheckingForm.setDictId(dictId);
		return reviewerCheckingForm;
	}
	
	public BaseForm procDiscard(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String[] ids = request.getParameterValues("wordID");
		WordsDAO wordDAO = new WordsDAO();
		for(int i=0; i < ids.length; i++) {
			wordDAO.deleteWord(Integer.parseInt(ids[i]));
		}
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		List<WordsDTO> listWord = wordDAO.getWord(dictId);
		ReviewerCheckingForm reviewerCheckingForm = new ReviewerCheckingForm();
		List<WordsDTO> listNewWords = new ArrayList<WordsDTO>();
		for (WordsDTO wordsDTO : listWord) {
			if(wordsDTO.getStatus() == 0) {
				listNewWords.add(wordsDTO);
			}
		}
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from;
		LanguagesDTO to;
		int fromId = dictDTO.getFrom();
		int toId = dictDTO.getTo();
		from = lanDAO.getLanguage(fromId);
		to = lanDAO.getLanguage(toId);
		reviewerCheckingForm.setDictName(from.getName()+" - "+to.getName());
		reviewerCheckingForm.setListNewWords(listNewWords);
		reviewerCheckingForm.setDictId(dictId);
		return reviewerCheckingForm;
	}
}
